import re

from django.contrib.auth.models import Permission, Group
from django.contrib.contenttypes.models import ContentType
from rest_framework import serializers
from users.models import User

class PermissionSerializer(serializers.ModelSerializer):
    '''权限序列化器类'''
    class Meta:
        model=Permission
        fields="__all__"


class ContentTypeSerializer(serializers.ModelSerializer):
    '''权限类型序列化器类'''
    class Meta:
        model=ContentType
        fields=('id','name')

class GroupSerializer(serializers.ModelSerializer):
    """用户组序列化器类"""
    class Meta:
        model = Group
        fields = '__all__'

class PermissionSimpleSerializer(serializers.ModelSerializer):
    """用户组序列化器类"""

    class Meta:
        model = Group
        fields =('id','name')

class AdminSerializer(serializers.ModelSerializer):
    """管理员用户序列化器类"""
    class Meta:
        model = User
        fields = ('id', 'username', 'mobile', 'email',
                  'groups', 'user_permissions', 'password')


        extra_kwargs={
            'password':{
                'write_only':True,
                'required':False,
                'allow_blank':True

            }
        }

    def validate_mobile(self,value):
        if not re.match(r'^1[3-9]\d{9}$',value):
            raise serializers.ValidationError('手机格式不正确')

        if self.context['view'].action=='creat':
            count=User.objects.filter(mobile=value).count()

            if count>0:
                raise serializers.ValidationError('手机号已存在')
        return value

    def  create(self, validated_data):
        '''创建管理员用户'''
        #设置管理员标记 is_staff为True
        validated_data['is_staff']=True

        #创建新的管理员用户
        user=super().create(validated_data)

        #密码加密保存
        password=validated_data['password']
        if not password:
           #管理员默认密码
            password='123abc'

        user.set_password(password)
        user.save()
        return user

